const Router = require('@koa/router');
const router = new Router();
const { verify } = require('../lib/jwt.js');
const {findPetListBytype,publishPet}=require('../controllers/index.js')

router.get('/findPetListBytype',verify(), async(ctx)=>{

    
 const{pet_type}=ctx.request.query
 console.log(123)
 try{
    const res=await findPetListBytype(pet_type)
    console.log(res);
    if(res.length>0){
        ctx.body={
            code:'1',
            msg:'查找成功',
            data:res
        }
    }else{
          ctx.body={
            code:'1',
            msg:'暂无数据',
            data:[]
        }
    }

    
 }catch(error){
    ctx.body={
        code:'-1',
        msg:'服务器异常',
        data:error
    }
 }
 

})
router.post('/petpublish',verify(),async(ctx)=>{

  const {pet_content,pet_img,pet_type,pet_breed,pet_contact,pet_feature}=ctx.request.body
  try {
    const res=await publishPet({
        pet_content,
        pet_img,
        pet_type,
        pet_breed,
        pet_contact,
        pet_feature,
        update_time:new Date(),
        create_time:new Date()
    })
      console.log(res,'res')
    if (res.affectedRows > 0) {
      ctx.body = {
        code: '1',
        msg: '发布成功',
        data: res
      }
    } else {
      ctx.body = {
        code: '0',
        msg: '发布失败',
        data: []
      }
    }
    
  } catch (error) {
     ctx.body = {
      code: '-1',
      msg: '服务器异常',
      data: error
     }
  }

})


module.exports = router;